package com.study.springcache.controller;

import com.study.springcache.common.config.CommonConfig;
import com.study.springcache.service.CacheService;
import com.study.springcache.service.CacheTestService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @Description:
 * @Author: 刘超
 * @Date: 2023-03-01 9:55
 */
@RestController
@RequestMapping("/cache")
public class CacheController {

    @Autowired
    CacheService cacheService;

    @Autowired
    CacheTestService cacheTestService;

    @Autowired
    CommonConfig commonConfig;

    private static Logger logger = LoggerFactory.getLogger(CacheController.class);


    @GetMapping("/cacheTest1")
    public String cacheTest1(@RequestParam(name = "id", required = false) int id) {
        return cacheService.getData(id).getMsg();
    }

    @GetMapping("/cacheTest2/{id}")
    public String cacheTest2(@PathVariable(name = "id", required = false) int id) {
        return cacheService.getData(id).getMsg();
    }


    @RequestMapping(value = "/getPostData", produces = "application/json;charset=UTF-8")
    public Object getPostData(@RequestBody String requestBody) {
        logger.info(requestBody);
        return "success";
    }

    @GetMapping("/switch")
    public String switcher() {
        boolean flag = !commonConfig.isCache();
        commonConfig.setCache(flag);
        return "switch cache to [" + flag + "] at" + LocalDateTime.now();
    }

    @GetMapping("/redisCahce")
    public String redisCache(@RequestParam(name = "key", required = false) int key) {
        return cacheTestService.getRedisCacheData(key);
    }

    @GetMapping("/caffeineCache")
    public String caffeineCache(@RequestParam(name = "key", required = false) int key) {
        return cacheTestService.getCaffeineCacheData(key);
    }

    @GetMapping("/setVersion")
    public String setVersion(@RequestParam(name = "key") String key,
                             @RequestParam(name = "version", required = false) String version) {
        return cacheTestService.setVersion(key, version);
    }

    @GetMapping("/getVersion")
    public String getVersion(@RequestParam(name = "key") String key){
        return cacheTestService.getVersion(key);
    }

    @GetMapping("/setData")
    public String setData(@RequestParam(name = "key") String key,
                          @RequestParam(name = "version", required = false) String version,
                          @RequestParam(name = "data") String data){
        if (!StringUtils.hasLength(version)) {
            version = cacheTestService.getVersion(key);
        }
        return cacheTestService.setData(key, version, data);
    }

    @GetMapping("/getData")
    public String getData(@RequestParam(name = "key") String key,
                          @RequestParam(name = "version", required = false) String version){
        if (!StringUtils.hasLength(version)) {
            version = cacheTestService.getVersion(key);
        }
        return cacheTestService.getData(key, version);
    }


}
